package com.bsh.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 加解密3次
 * 
 * @author gsh
 *
 */
public class Des3 {
	/**
	 * 加密3次
	 * 
	 * @param encStr
	 * @return
	 */
	public static String enc(String encStr) {
		String jmStr = encStr;
		for (int i = 0; i < 3; i++) {
			try {
				jmStr = DesUtils2.encryptDES(jmStr, DesUtils2.key);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return jmStr;
	}

	/**
	 * 解密3次
	 * 
	 * @param decStr
	 * @return
	 */
	public static String dec(String decStr) {
		String jmStr = decStr;
		for (int i = 0; i < 3; i++) {
			try {
				jmStr = DesUtils2.decryptDES(jmStr, DesUtils2.key);
				System.out.println("第" + Integer.toString(i) + "次解密：" + jmStr);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return jmStr;
	}

	/**
	 * 解密客户端请求参数
	 * 
	 * @param paramStr
	 * @param retClass
	 * @return
	 */
	public static <T> T decRequest(String paramStr, Class<T> retClass) {
		if (true) {
			Gson gson = new Gson();
			T dto = gson.fromJson(paramStr, retClass);
			return dto;
		}
		String jsonStr = Des3.dec(paramStr);
		Gson gson = new Gson();
		T dto = gson.fromJson(jsonStr, retClass);
		return dto;
	}

	/**
	 * 加密响应
	 * 
	 * @param data
	 * @return
	 */
	public static String encResponse(Object data) {
		// Gson gson=new Gson();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		String json = gson.toJson(data);
		// String encStr = Des3.enc(json);
		// 先不加密
		String encStr = json;
		return encStr;
	}
}
